//
//  MapDemoViewController.m
//  MapDemo
//
//  Created by grant on 12/1/09.
//  Copyright XMobileApp 2009. All rights reserved.
//

#import "MapDemoViewController.h"
#import "MapMark.h"

@implementation MapDemoViewController
@synthesize mapView;
@synthesize mapTypeControl;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	mark = [[MapMark alloc]initWithLatitude:31.022541 longitude:121.446890 title:@"Mark" subTitle:@"SubTitle"];
    
    float zoomLevel = 0.018;
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = 31.022541;
    coordinate.longitude = 121.446890;
    
    MKCoordinateRegion region =MKCoordinateRegionMake(coordinate,MKCoordinateSpanMake(zoomLevel,zoomLevel));
    [mapView setRegion:[mapView regionThatFits:region] animated:YES];

    
	[mapView addAnnotation:mark];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[mapView release];
	[mapTypeControl release];
    [super dealloc];
}

- (IBAction)changeMapType: (id)sender
{
	NSInteger index = [(UISegmentedControl *)sender selectedSegmentIndex];
	switch (index) {
		case 0:
			mapView.mapType = MKMapTypeStandard;
			break;
		case 1:
			mapView.mapType = MKMapTypeHybrid;
			break;
		case 2:
			mapView.mapType = MKMapTypeSatellite;
			break;
		default:
			break;
	}
}

#pragma mark -
#pragma mark MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
	MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation
																  reuseIdentifier:@"locationMark"];
	[annView setPinColor:MKPinAnnotationColorPurple];
	UIButton *btn = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
	[btn setFrame:CGRectMake(0, 0, 20, 20)];
	[btn addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
	annView.rightCalloutAccessoryView = btn;
	annView.canShowCallout = YES;
	annView.animatesDrop = YES;
	return annView;
}

- (void)showDetails: (id)sender
{
	MKCoordinateRegion region = MKCoordinateRegionMake(mark.coordinate, 
													   MKCoordinateSpanMake(0.015, 0.015));
	[mapView setRegion:region animated:YES];
}

-(IBAction)OK
{
    [self dismissModalViewControllerAnimated:YES];
}

@end
